﻿using System;
using System.Collections.Generic;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Manipulation
{
    public class CommandRotateComponent : SerializedUndoRedo, ICommand
    {
        private ModelControl modelControl;
        private List<Type> typesToRotate;
        private Angle angle;
        private bool rotateWithCopy;

        public CommandRotateComponent(ModelControl modelControl, Type[] typesToRotate, Angle angle, bool rotateWithCopy)
        {
            this.modelControl = modelControl;
            this.typesToRotate = new List<Type>(typesToRotate);
            this.angle = (Angle)angle.Clone();
            this.rotateWithCopy = rotateWithCopy;
        }

        #region ICommand Members

        public void StartExecute()
        {
            
            this.undoModelContainer = (ModelContainer)modelControl.ModelContainer.Clone();
            foreach (IComponent innerComponent in modelControl.ModelContainer.AllInnerComponents)
            {
                if (innerComponent.Selected && this.typesToRotate.Contains(innerComponent.GetType()))
                {
                    IComponent componentToRotate = null;
                    if (rotateWithCopy)
                    {
                        componentToRotate = (IComponent)innerComponent.Copy();
                        componentToRotate.SerialNumber = 0;
                        innerComponent.Parent.AddInnerComponent(componentToRotate);                        
                    }
                    else
                        componentToRotate = innerComponent;

                    componentToRotate.Rotate(angle.Value);
                }
            }
        }

        public void Execute()
        {
            
        }

        public void EndExecute()
        {
            
        }

        public bool IsNeedModelUpdate
        {
            get { return true; }
        }

        public string Name
        {
            get { return "Вращение компонента"; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            CommandRotateComponent clone = new CommandRotateComponent(this.modelControl, this.typesToRotate.ToArray(), this.angle, rotateWithCopy);
            if (this.undoModelContainer != null)
                clone.undoModelContainer = (ModelContainer)this.undoModelContainer.Clone();
            if (this.redoModelContainer != null)
                clone.redoModelContainer = (ModelContainer)this.redoModelContainer.Clone();

            return clone;
        }

        #endregion

        public override string ToString()
        {
            return this.Name;
        }

        #region ICommand Members


        private bool enabled = true;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        #endregion

        #region ICommand Members


        public bool IsNeedVisitorsUpdate
        {
            get { return true; }
        }

        #endregion
    }
}
